/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName CompanyManager.as
 * @creationDate Sep 5, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model
{
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	import com.gesturesoft.imultimax.model.vo.StyleVO;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.styles.CSSStyleDeclaration;

	public class CompanyManager
	{
		
		private var _selectedCompany:String;
		private var styleCollection:ArrayCollection;
		
		public function CompanyManager()
		{
			
			
		}
		
		/**
		 * This is the function for change company theme
		 * 
		 * @functionName changeCompanyTheme
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params arg:String
		 */
		public function changeCompanyTheme(selectedCompany:String):void
		{
			
			_selectedCompany = selectedCompany;
			
			gettingDetails();
			
			var formattedStyleArray:Array = formatStyles();
			applyTheme( formattedStyleArray );
			
			removeCurrentThemeApplied();
			
		}
		
		/**
		 * This is the function for getting details for applying the styles
		 * 
		 * @functionName gettingDetails
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params 
		 */
		private function gettingDetails():void
		{
			
			getStyleDetails();
			getAssets();
			setCurrentCompany();
			getCurrency();
			
		}
		/**
		 * This is the function for getting the currency selected
		 * 
		 * @functionName getCurrency
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 */
		private function getCurrency():void
		{
			
			var query:String;
			
			//START:Getting currency used by the company
			query = "SELECT cmp_currency FROM IM_COMPANIES WHERE cmp_company='"+_selectedCompany+"'";
			var currency:ArrayCollection = retrieveHandler(query);
			if( currency.length > 0)
				AppModel.getInstance().currency = currency[0].cmp_currency;
			else
				AppModel.getInstance().currency = '£';
			//END:Getting currency used by the company
			
		}
		
		/**
		 * This is the function for set the current company as default company
		 * 
		 * @functionName setCurrentCompany
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 */
		private function setCurrentCompany():void
		{
			
			//START:Update startup company
			executeHandler("UPDATE IM_COMPANIES SET cmp_startup='0'");
			executeHandler("UPDATE IM_COMPANIES SET cmp_startup='1' WHERE cmp_company='"+_selectedCompany+"'");
			//END:Update startup company
			
		}
		
		/**
		 * This is the function for getting style details
		 * 
		 * @functionName getStyleDetails
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params :String
		 */
		private function getStyleDetails():ArrayCollection
		{
			
			var query:String;
			styleCollection = new ArrayCollection();
			
			//START:Get style detials from db
			query = "SELECT * FROM IM_STYLES WHERE st_company='"+_selectedCompany+"'";
			styleCollection = retrieveHandler(query);
			//END:Get style detials from db
			
			return styleCollection;
		}
		
		/**
		 * This is the function for getting asset details
		 * 
		 * @functionName getAssets
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params arg:String
		 */
		private function getAssets():void
		{
			var query:String;
			var assetCollection:ArrayCollection = new ArrayCollection();
			
			//START:Get asset detials from db and store it to AppModel
			query = "SELECT * FROM IM_ASSETS WHERE as_company='"+_selectedCompany+"'";
			assetCollection = retrieveHandler(query);
			storeAssetValues(assetCollection);
			
			AppModel.getInstance().assets = assetCollection;
			//END:Get asset detials from db
			
		}
		
		/**
		 * This is the function for store asset values to model class
		 * 
		 * @functionName storeAssetValuesvate function storeAssetValues(assetsArray:ArrayCollection):void

		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params assetCollection:ArrayCollection
		 */
		private function storeAssetValues(assetCollection:ArrayCollection):void
		{
			
			var obj:Object = new Object();
			var appModel:AppModel = AppModel.getInstance();
			var backgroundImage:String;
			
			for each(var item2:Object in assetCollection)
			{
				obj[item2.as_type+item2.as_name] = item2.as_value.toString();
			}
			
			
			AppModel.getInstance().assetObj = obj;
			
		}
		
		//------------------------START:Applying styles--------------------------------
		
		/**
		 * This is the function for apply theme
		 * 
		 * @functionName applyTheme
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params arg:String
		 */
		private function applyTheme(formattedStyleArray:Array):void
		{
			
			for each(var arrCol2:ArrayCollection in formattedStyleArray)
			{
				var cssStyle2:CSSStyleDeclaration = new CSSStyleDeclaration();
				var className2:String;
				
				for each(var styleVo2:StyleVO in arrCol2)
				{
					
					if(!className2)
						className2 = styleVo2.className;
					cssStyle2.setStyle(styleVo2.property,styleVo2.value);
					
				}
				
				FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration(className2, cssStyle2, true);
				className2 = null;
			}
			
		}
		
		private function formatStyles():Array
		{
			
			var formatedArray:Array = new Array();
			
			for each(var item:Object in styleCollection)
			{
				
				if(!formatedArray[item.st_class_name])
				{
					formatedArray[item.st_class_name] = new ArrayCollection();
				}
				
				var styleVo:StyleVO = new StyleVO();
				
				styleVo.className = item.st_class_name;
				styleVo.property = item.st_property;
				styleVo.value = item.st_value;
				styleVo.activeTablePropery = item.st_active_table_propery;
				styleVo.activeTableSubPropery = item.st_active_table_sub_propery;
				
				ArrayCollection( formatedArray[item.st_class_name] ).addItem(styleVo);
			}
			
			return formatedArray;
			
		}
		
		/**
		 * This is the function for remove the current active theme
		 * 
		 * @functionName removeCurrentThemeApplied
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 */
		private function removeCurrentThemeApplied():void
		{
			
			var query:String = "UPDATE IM_THEMES SET th_active='false'";
			executeHandler( query );
			
		}
		
		//------------------------END:Applying styles--------------------------------
		
		/**
		 * This is the function for retrieving datas from database as collection of arrays
		 * 
		 * @functionName retreiveHandler
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params query:String
		 */
		private function retrieveHandler(val:String):ArrayCollection
		{
			
			var iDataBaseManager:IDatabaseManager = new DataBaseManager();
			var result:ArrayCollection;
			
			try
			{
				
				result =  iDataBaseManager.getData(val);
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace("ApplicationStyleUpdator-retrieveHandler() : Database Read error");
				throw error;
				
			}
			
			return result;
			
		}
		
		/**
		 * This is the function for execute query
		 * 
		 * @functionName executeHandler
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params val:String
		 */
		private function executeHandler(val:String):void
		{
			
			var iDatabaseManager:IDatabaseManager = new DataBaseManager();
			try
			{
				
				iDatabaseManager.execute(val);
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace("ApplicationStyleUpdator-executeHandler() : Database execution error");
				throw error;
				
			}
			
		}
		
	}
}